package com.mzba.zxdlyllp;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.waps.AdView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.mzba.zxdlyllp.db.SexTipsTable.SexTipsEntity;
/**
 * 
 * @author 06peng
 *
 */
public class SexTipsSlidingSettingFragment extends SherlockFragment {

	private ListView listView;
	private SextipsActivity mainActivity;
	private LinearLayout userLayout;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mainActivity = (SextipsActivity) getSherlockActivity();
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_sm_list, null);
		listView = (ListView) view.findViewById(R.id.list);
		userLayout = (LinearLayout) view.findViewById(R.id.AdLinearLayout);
		new AdView(mainActivity, userLayout).DisplayAd();
		return view;
	}
	

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		NavFragmentItemAdapter adapterIndex = new NavFragmentItemAdapter(mainActivity);
		if (mainActivity.list != null && !mainActivity.list.isEmpty()) {
			for (SexTipsEntity entity : mainActivity.list) {
				adapterIndex.add(new NavFragmentItem(entity.getTitle(), entity.getContent()));
			}
		}
		
		
		listView.setAdapter(adapterIndex);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				mainActivity.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		        NavFragmentItem item = (NavFragmentItem) listView.getAdapter().getItem(arg2);
		        
		        FragmentNavigation.replace(mainActivity, new SexTipsFragmentItem(item.content), R.id.content, item.tag);
		        
		        mainActivity.getSlidingMenu().toggle();
	        	mainActivity.actionBar.setTitle(item.tag);
		        	
			}
		});
	}

	private class NavFragmentItem {
		public String tag;
		public byte[] content;
		public NavFragmentItem(String tag, byte[] content) {
			this.tag = tag; 
			this.content = content;
		}
	}

	public class NavFragmentItemAdapter extends ArrayAdapter<NavFragmentItem> {

		public NavFragmentItemAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_sm_list_item, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
//			icon.setImageResource(getItem(position).content);
			icon.setVisibility(View.GONE);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);

			return convertView;
		}
	}
	
	
	@Override 
	public void onDestroy() {
		super.onDestroy();
	}
	
}
